package com.ltf.controller;

import com.ltf.entity.*;
import com.ltf.service.*;
import com.ltf.entity.*;
import com.ltf.entity.*;
import com.ltf.utils.PackJson;
import com.ltf.service.*;
import com.ltf.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController //@ResponseBody+@Controller 可以省去下面每个方法都加ResponseBody
@RequestMapping("/api/public")
public class PublicController {
    @Autowired
    LargeFileService largeFileService;
    @Autowired
    ClzService clzService;
    @Autowired
    CourseService courseService;
    @Autowired
    XUserService xUserService;
    @Autowired
    CarouselService carouselService;

    public PublicController(){
    }

    //图片显示
//    @GetMapping("/showing/{id}")
//    public void dispaly(@PathVariable String id, HttpServletResponse resp)throws Exception{
//        System.out.println(id);
//        LargeFile one = largeFileService.findOne(id);
//        System.out.println("snduankjn"+one);
//        Object content = one.getContent();
//        ServletOutputStream os = resp.getOutputStream();
//        resp.setContentType("image/jpg");
//        byte[] buf = (byte[]) content;
//        os.write(buf);
//        os.flush();
//    }


    @GetMapping("/showing/{id}")
public void dispaly(@PathVariable String id, HttpServletResponse resp) throws Exception {
    System.out.println("ID: " + id);

    // 调用服务方法获取 LargeFile 对象
    LargeFile one = largeFileService.findOne(id);

    // 检查是否找到对应的 LargeFile
    if (one == null) {
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND); // 返回 404 状态码
        resp.getWriter().write("File not found for ID: " + id);
        return;
    }

    System.out.println("Found file: " + one);

    // 获取文件内容并检查是否为 null
    Object content = one.getContent();
    if (content == null || !(content instanceof byte[])) {
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // 返回 500 状态码
        resp.getWriter().write("File content is invalid or missing.");
        return;
    }

    // 设置响应头和写入数据
    resp.setContentType("image/jpg");
    ServletOutputStream os = resp.getOutputStream();
    byte[] buf = (byte[]) content;
    os.write(buf);
    os.flush();
}


    //班级显示
    @GetMapping("/getClzs")
    public XJson getClzs(){
        List<Clz> clzs = this.clzService.findAll();
        return PackJson.packX(200,"",clzs);
    }

    //课程显示
    @GetMapping("/getCourses")
    public XJson getCourses(){
        List<Course> courses = this.courseService.findAll();
        return PackJson.packX(200,"",courses);
    }

    //教师显示
    @GetMapping("/getTeas")
    public XJson getTeas(){
        List<XUser> teas = xUserService.findByRole("tea");
        return PackJson.packX(200,"",teas);
    }

    @GetMapping("/getCarousel")
    public XJson findAll(){
        List<Carousel> all = carouselService.findAll();
        return PackJson.packX(200,"",all);
    }

    @GetMapping("/getStu")
    public XJson findStu(){
        List<XUser> stu = xUserService.findByMax("stu");
        System.out.println(stu);
        return PackJson.packX(200,"",stu);
    }
    @GetMapping("/getTea")
    public XJson findTea(){
        List<XUser> tea = xUserService.findByMax("tea");
        return PackJson.packX(200,"",tea);
    }
}
